C++ cin and cout: A Basic Tutorial on Input/Output Streams

This article introduces the basic methods for input and output in C++ using `cin` and `cout`. Input and output streams are provided by the `<iostream>` library, which needs to be included, and the statement `using namespace std;` is used to simplify the code. `cin` reads data from the keyboard using the extraction operator `>>`, with the syntax `cin >> variable`. It supports types such as integers and floating-point numbers, for example, reading an age into an `int` variable. `cout` outputs data using the insertion operator `<<`, supporting continuous output with the syntax `cout << data1 << data2`. It can output strings, numbers, etc. To read a string with spaces, `getline(cin, string variable)` should be used (with the `<string>` header file included). Notes include: variables must be defined before input, data types must match, the header file must not be omitted, and continuous input can be separated by spaces. Mastering the operators of `cin`/`cout` and data type handling (such as `getline`) enables implementing basic input and output functions.

Read More